﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.UI.Models;
using ARR.Entities;
using System.Collections.ObjectModel;
using ARR.Shared;
using System.Windows.Input;
using ARR.UI.Commands;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.Reflection;
using DDF.Entity;

namespace ARR.UI.ViewModels
{
    public class AttributesMasterViewModel : WorkspaceViewModel
    {
        private IAttributesMasterModel model;
        private ICommand selectionChangedCommand;
        private ICommand saveCommand;
        private ICommand cancelCommand;
        private ICommand deleteCommand;
        private ICommand addingNewRowCommand;
        private string editedName;

        
        
        public AttributesMasterViewModel(IAttributesMasterModel model)
        {
            base.DisplayName = Labels.MaestroDeAtributos;
            this.model = model;

            SelectedAttributes = new List<AttributeType>();
        }



        public ICommand SelectionChangedCommand
        {
            get
            {
                if (selectionChangedCommand == null)
                {
                    selectionChangedCommand = new RelayCommand(e => GetSelectedItems(e as SelectionChangeEventArgs));
                }
                return selectionChangedCommand;
            }
        }

        public ICommand AddingNewRowCommand
        {
            get
            {
                if (addingNewRowCommand == null)
                {
                    addingNewRowCommand = new RelayCommand(e => OnAddingNewRow(e as GridViewAddingNewEventArgs));
                }
                return addingNewRowCommand;
            }
        }
                
        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(e => Save());
                }
                return saveCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new RelayCommand(e => Cancel());
                }
                return cancelCommand;
            }
        }

        public ICommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new RelayCommand(e => Delete());
                }
                return deleteCommand;
            }
        }


        public ObservableCollection<AttributeType> AttributeTypes
        {
            get
            {
                return new ObservableCollection<AttributeType>(model.AttributeTypes.Where(x => !x.IsArticleBaseColumn));
            }
        }

        public ObservableCollection<AttributeType> ArticlesAttributes
        {
            get
            {
                return new ObservableCollection<AttributeType>(model.AttributeTypes.Where(x => x.IsArticleBaseColumn));
            }
        }

        public List<AttributeType> SelectedAttributes { get; set; }

        public Array ArticleNumericColumns
        {
            get
            {
                return Enum.GetValues(typeof(ArticleNumericColumns));
            }
        }

        public ObservableCollection<Category> Categories
        {
            get
            {
                var cats = model.Categories.OrderBy(x => x.Code).ToList();
                cats.Insert(0, new Category { Id = 0, Code = "000000", Name = "Ninguna" });
                return new ObservableCollection<Category>(cats);
            }
        }
        

        private void GetSelectedItems(SelectionChangeEventArgs e)
        {
            try
            {
                List<AttributeType> added = e.AddedItems.Cast<AttributeType>().ToList();
                List<AttributeType> removed = e.RemovedItems.Cast<AttributeType>().ToList();

                SelectedAttributes.AddRange(added);
                removed.ForEach(at => SelectedAttributes.Remove(at));
            }
            catch
            {
            }
        }

        private void OnAddingNewRow(GridViewAddingNewEventArgs e)
        {
            AttributeType newAttribute = new AttributeType();
            newAttribute.Values = new List<Entities.Attribute>();

            if (e.OwnerGridViewItemsControl.Name == "ArticleAttributeGV")
            {
                newAttribute.IsArticleBaseColumn = true;
            }
            else
            {
                newAttribute.IsArticleBaseColumn = false;
            }

            e.NewObject = newAttribute;
            model.AttributeTypes.Add(newAttribute);
        }

        private void Save()
        {
            if (AreValid())
            {
                model.Save();
                model.LoadAttributeTypes();

                OnPropertyChanged<AttributesMasterViewModel>(p => p.AttributeTypes);
            }
        }

        private void Cancel()
        {
            model.LoadAttributeTypes();

            OnPropertyChanged<AttributesMasterViewModel>(p => p.AttributeTypes);
        }
        
        private void Delete()
        {

        }

        private bool AreValid()
        {
            foreach (AttributeType attribute in AttributeTypes)
            {
                if (!attribute.IsValid)
                    return false;
            }

            return true;
        }

    }
}
